package cn.tedu.tea.service;

import cn.tedu.tea.domain.TStockRecordOut;
import cn.tedu.tea.domain.param.TStockRecordOutAddNewDTO;
import cn.tedu.tea.domain.param.TStockRecordOutUpdateDTO;
import cn.tedu.tea.domain.vo.TStockRecordOutListVO;
import cn.tedu.tea.domain.vo.TStockRecordOutStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.TStockRecordOutMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class TStockRecordOutServiceImpl implements ITStockRecordOutService {

    @Autowired
    private TStockRecordOutMapper tStockRecordOutMapper;

    @Override
    public void addNew(TStockRecordOutAddNewDTO tStockRecordOutAddNewDTO) {
        log.debug("开始处理【添加库存信息】的业务，参数： {}", tStockRecordOutAddNewDTO);
        TStockRecordOut tStockRecordOut = new TStockRecordOut();
        BeanUtils.copyProperties(tStockRecordOutAddNewDTO, tStockRecordOut);
        int rows = tStockRecordOutMapper.insert(tStockRecordOut);
        if (rows != 1){
            String message = "添加库存信息失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除库存信息】的业务，参数： {}", id);
        TStockRecordOutStandardVO tStockRecordOutStandard = tStockRecordOutMapper.getStandardById(id);
        if (tStockRecordOutStandard == null){
            String message = "删除库存信息失败，要删除的库存信息不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = tStockRecordOutMapper.deleteById(id);
        if (rows != 1){
            String message = "删除库存信息失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Long[] ids) {
        log.debug("开始处理【根据id批量删除库存信息】的业务，参数： {}", ids);
        int rows = tStockRecordOutMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除库存信息失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(TStockRecordOutUpdateDTO tStockRecordOutUpdateDTO) {
        log.debug("开始处理【修改库存信息信息】的业务，参数：{}", tStockRecordOutUpdateDTO);
        TStockRecordOutStandardVO tStockRecordOutStandard = tStockRecordOutMapper.getStandardById(tStockRecordOutUpdateDTO.getId());
        if (tStockRecordOutStandard == null){
            String message = "修改库存信息失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        TStockRecordOut tStockRecordOut = new TStockRecordOut();
        BeanUtils.copyProperties(tStockRecordOutUpdateDTO, tStockRecordOut);
        int rows = tStockRecordOutMapper.update(tStockRecordOut);
        if (rows != 1){
            String message = "修改库存信息失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public TStockRecordOutStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询库存信息详情】的业务，参数： {}", id);
        TStockRecordOutStandardVO tStockRecordOutStandard = tStockRecordOutMapper.getStandardById(id);
        if (tStockRecordOutStandard == null){
            String message = "查询库存信息详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return tStockRecordOutStandard;
    }

    @Override
    public List<TStockRecordOutListVO> list() {
        log.debug("开始处理【查询库存信息列表】的业务，无参数");
        List<TStockRecordOutListVO> list = tStockRecordOutMapper.list();
        return list;
    }
    
}
